package com.sofwin.controller;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.sofwin.pojo.Enterprise;
import com.sofwin.service.EnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/enterprise")
public class EnterpriseController extends BaseController {
    @Autowired
    private EnterpriseService enterpriseService;

    @GetMapping("/entList")
    public TableDataInfo entList(@RequestBody Enterprise enterprise,
                                 @RequestParam(name = "pageCurrent",required = true,defaultValue = "1") Integer pageCurrent,
                                 @RequestParam(name = "pageSize",required = true,defaultValue = "5")Integer pageSize){
        LambdaQueryWrapper<Enterprise> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(enterprise.getEName())){
            wrapper.like(Enterprise::getEName,enterprise.getEName());
        }
        if(StringUtils.isNotEmpty(enterprise.getEWebsite())){
            wrapper.eq(Enterprise::getEWebsite,enterprise.getEWebsite());
        }
        Page<Enterprise> page = enterpriseService.page(new Page<>(pageCurrent, pageSize), wrapper);
        return getDataTable(page.getRecords(),page.getTotal());
    }

    @PostMapping("/addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Enterprise enterprise){
        if (enterprise.getEId()==0&&StringUtils.isEmpty(enterpriseService.getById(enterprise.getEId()).toString())){
            enterpriseService.save(enterprise);
            return AjaxResult.success("新增成功");
        }
        enterpriseService.updateById(enterprise);
        return AjaxResult.success("修改成功");
    }

    @DeleteMapping("delEnt")
    public AjaxResult delEnt(@RequestBody List<Integer> ids){
        enterpriseService.removeByIds(ids);
        return AjaxResult.success("批量删除成功");
    }
}
